using System;
using System.Collections.Generic;
using System.Text;
using TomShane.Neoforce.Controls;
using Base;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Interface
{
    public class SideBar
    {
        private Control panel = null;
        private Manager manager;

        private Control grpStats = null;
        private Label lblObjects = null;
        private Label lblAvgFps = null;
        private Label lblFrames = null;
        private Label lblFps = null;
        private Label lblMouse = null;
        private Label lblProj = null;
        int afps = 0;
        int fps = 0;
        double et = 0;
        private Control grpRes = null;
        private CheckBox chkRes1024 = null;
        private CheckBox chkRes1280 = null;
        private CheckBox chkResFull = null;

        private List<CheckBox> units = new List<CheckBox>();
        List<string> unitTypes;
        private Button btnApply = null;
        private Button btnExit = null;

        public SideBar(Manager mainManager, List<string> unitTypesIn)
        {
            unitTypes = unitTypesIn;
            manager = mainManager;
            panel = new Control(manager);
            panel.Init();
            panel.Skin = manager.Skin.Controls["Panel"];
            panel.Width = 200;
            panel.Height = GameInformation.Configuration.Height;
            InitStats();
            InitRes();
            InitUnit();
            manager.Add(panel);
        }

        private void InitStats()
        {

            grpStats = new Control(manager);
            grpStats.Init();
            grpStats.Skin = manager.Skin.Controls["PanelGroup"];
            grpStats.Left = 16;
            grpStats.Width = panel.Width - grpStats.Left;
            grpStats.Height = 106;
            grpStats.Top = 80;
            //grpStats.Top = GameInformation.Configuration.Height - 16 - grpStats.Height;
            grpStats.Anchor = Anchor.Left;// Anchor.Bottom;
            grpStats.CanFocus = false;

            lblObjects = new Label(manager);
            lblObjects.Init();
            lblObjects.Left = 8;
            lblObjects.Top = 8;
            lblObjects.Height = 16;
            lblObjects.Width = grpStats.Width - lblObjects.Left * 2;
            lblObjects.Alignment = Alignment.MiddleLeft;
            lblObjects.Color = new Color(255, 255, 255, 255);
            lblObjects.Text = "Statistics";

            lblFrames = new Label(manager);
            lblFrames.Init();
            lblFrames.Left = 8;
            lblFrames.Top = 24;
            lblFrames.Height = 16;
            lblFrames.Width = grpStats.Width - lblObjects.Left * 2;
            lblFrames.Alignment = Alignment.MiddleLeft;
            lblFrames.Color = new Color(255, 255, 255, 220);

            lblAvgFps = new Label(manager);
            lblAvgFps.Init();
            lblAvgFps.Left = 8;
            lblAvgFps.Top = 40;
            lblAvgFps.Height = 16;
            lblAvgFps.Width = grpStats.Width - lblObjects.Left * 2;
            lblAvgFps.Alignment = Alignment.MiddleLeft;
            lblAvgFps.Color = new Color(255, 255, 255, 220);

            lblFps = new Label(manager);
            lblFps.Init();
            lblFps.Left = 8;
            lblFps.Top = 56;
            lblFps.Height = 16;
            lblFps.Width = grpStats.Width - lblObjects.Left * 2;
            lblFps.Alignment = Alignment.MiddleLeft;
            lblFps.Color = new Color(255, 255, 255, 220);

            lblMouse = new Label(manager);
            lblMouse.Init();
            lblMouse.Left = 8;
            lblMouse.Top = 72;
            lblMouse.Height = 16;
            lblMouse.Width = grpStats.Width - lblObjects.Left * 2;
            lblMouse.Alignment = Alignment.MiddleLeft;
            lblMouse.Color = new Color(255, 255, 255, 220);

            lblProj = new Label(manager);
            lblProj.Init();
            lblProj.Left = 8;
            lblProj.Top = 86;
            lblProj.Height = 16;
            lblProj.Width = grpStats.Width - lblObjects.Left * 2;
            lblProj.Alignment = Alignment.MiddleLeft;
            lblProj.Color = new Color(255, 255, 255, 220);


            grpStats.Add(lblObjects);
            grpStats.Add(lblFrames);
            grpStats.Add(lblAvgFps);
            grpStats.Add(lblFps);
            grpStats.Add(lblMouse);
            grpStats.Add(lblProj);
            panel.Add(grpStats);
        }
        public void UpdateControls(GameTime time)
        {
            if (et >= 500 || et == 0)
            {
                if (time.TotalGameTime.TotalSeconds != 0)
                {
                    afps = (int)(manager.Frames / time.TotalGameTime.TotalSeconds);
                }

                if (time.ElapsedGameTime.TotalMilliseconds != 0)
                {
                    fps = (int)(1000 / time.ElapsedGameTime.TotalMilliseconds);
                }

                et = 1;
            }
            et += time.ElapsedGameTime.TotalMilliseconds;

            lblFrames.Text = "Frames: " + manager.Frames.ToString();
            lblAvgFps.Text = "Average FPS: " + afps.ToString();
            lblFps.Text = "Current FPS: " + fps.ToString();
            lblMouse.Text = "Unit Count: " + GameInformation.UnitCount.ToString();
            lblProj.Text = "Projectile Count: " + GameInformation.ProjectileCount.ToString();
        }
        private void InitUnit()
        {
            Control grpUnit = new Control(manager);
            grpUnit.Init();
            grpUnit.Skin = manager.Skin.Controls["PanelGroup"];
            grpUnit.Left = 16;
            grpUnit.Top = 360;
            grpUnit.Width = panel.Width - grpRes.Left;
            grpUnit.Height = 86;
            grpUnit.CanFocus = false;
            grpUnit.Anchor = Anchor.Left;
            int i = 0;
            foreach (string name in unitTypes)
            {
                CheckBox newCheck = new CheckBox(manager);
                newCheck.Init();
                newCheck.Left = 8;
                newCheck.Width = grpRes.Width - chkRes1024.Left * 2;
                newCheck.Height = 16;
                newCheck.Text = name;
                newCheck.Top = 24 + (i * 16);
                newCheck.Checked = i == 0? true:false;
                newCheck.Click += new TomShane.Neoforce.Controls.EventHandler(chkUnit_Click);
                i++;
                grpUnit.Add(newCheck);
                units.Add(newCheck);
            }




            Button btnCreate = new Button(manager);
            btnCreate.Init();


            SkinControl sc = manager.Skin.Controls["Button"];
            btnCreate.Skin = sc;
            btnCreate.Left = grpRes.Left;
            btnCreate.Top = 508;
            btnCreate.Text = "Create Unit";
            btnCreate.Click += new TomShane.Neoforce.Controls.EventHandler(btnCreate_Click);
            btnCreate.Skin.Layers["Button"].Text.StateColors.Enabled = Color.BurlyWood;
            btnCreate.Color = Color.White;
            btnCreate.Anchor = Anchor.Left;



            panel.Add(btnCreate);
            panel.Add(grpUnit);
        }


        private void InitRes()
        {
            grpRes = new Control(manager);
            grpRes.Init();
            grpRes.Skin = manager.Skin.Controls["PanelGroup"];
            grpRes.Left = 16;
            grpRes.Top = 16;
            grpRes.Width = panel.Width - grpRes.Left;
            grpRes.Height = 86;
            grpRes.CanFocus = false;

            chkRes1024 = new CheckBox(manager);
            chkRes1024.Init();
            chkRes1024.Left = 8;
            chkRes1024.Width = grpRes.Width - chkRes1024.Left * 2;
            chkRes1024.Height = 16;
            chkRes1024.Text = "Resolution 1024x768";
            chkRes1024.Top = 24;
            chkRes1024.Checked = true;
            chkRes1024.Click += new TomShane.Neoforce.Controls.EventHandler(chkRes_Click);

            chkRes1280 = new CheckBox(manager);
            chkRes1280.Init();
            chkRes1280.Left = chkRes1024.Left;
            chkRes1280.Width = chkRes1024.Width;
            chkRes1280.Height = chkRes1024.Height;
            chkRes1280.Text = "Resolution 1280x1024";
            chkRes1280.Top = 40;
            chkRes1280.Click += new TomShane.Neoforce.Controls.EventHandler(chkRes_Click);

            chkResFull = new CheckBox(manager);
            chkResFull.Init();
            chkResFull.Left = chkRes1024.Left;
            chkResFull.Width = chkRes1024.Width;
            chkResFull.Height = chkRes1024.Height;
            chkResFull.Text = "Fullscreen Mode";
            chkResFull.Top = 64;

            btnApply = new Button(manager);
            btnApply.Init();

            SkinControl sc = new SkinControl(manager.Skin.Controls["Button"]);
            manager.Skin.Controls.Add(sc);

            btnApply.Skin = sc;
            btnApply.Left = grpRes.Left;
            btnApply.Top = 108;
            btnApply.Text = "Apply";
            btnApply.Click += new TomShane.Neoforce.Controls.EventHandler(btnApply_Click);
            btnApply.Skin.Layers["Button"].Text.StateColors.Enabled = Color.BurlyWood;
            btnApply.Color = Color.White;

            btnExit = new Button(manager);
            btnExit.Init();
            btnExit.Left = btnApply.Left + btnApply.Width + 4;
            btnExit.Top = 108;
            btnExit.Text = "Exit";
            btnExit.Click += new TomShane.Neoforce.Controls.EventHandler(btnExit_Click);

            grpRes.Add(chkRes1024);
            grpRes.Add(chkRes1280);
            grpRes.Add(chkResFull);

            panel.Add(grpRes);
            panel.Add(btnApply);
            panel.Add(btnExit);
        }
        ////////////////////////////////////////////////////////////////////////////

        ////////////////////////////////////////////////////////////////////////////
        void chkRes_Click(Control sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            chkRes1024.Checked = false;
            chkRes1280.Checked = false;

            (sender as CheckBox).Checked = true;
        }
        void chkUnit_Click(Control sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            foreach (CheckBox current in units)
            {
                current.Checked = false;
            }

            (sender as CheckBox).Checked = true;
        }
        ////////////////////////////////////////////////////////////////////////////

        ////////////////////////////////////////////////////////////////////////////    
        void btnExit_Click(Control sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            GameInformation.Game.Exit();
        }
        ////////////////////////////////////////////////////////////////////////////

        ////////////////////////////////////////////////////////////////////////////

        void btnCreate_Click(Control sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            GameInformation.CreateUnit = true;

            foreach (CheckBox current in units)
            {
                if (current.Checked)
                {
                    GameInformation.UnitType = current.Text;
                }
            }
        }
        void btnApply_Click(Control sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            GameInformation.ResChanged = true;
            GameInformation.Configuration.Fullscreen = chkResFull.Checked;
            int w = 1024;
            int h = 768;

            if (chkRes1024.Checked)
            {
                w = 1024;
                h = 768;
            }
            else if (chkRes1280.Checked)
            {
                w = 1280;
                h = 1024;
            }

            GameInformation.Configuration.Width = w;
            GameInformation.Configuration.Height = h;

        }

        public void ChangeRes()
        {


            ResChanged();
        }
        ////////////////////////////////////////////////////////////////////////////

        ////////////////////////////////////////////////////////////////////////////
        private void ResChanged()
        {
            panel.MaxHeight = GameInformation.Configuration.Height;
            panel.Height = GameInformation.Configuration.Height;
        }
    }
}
